﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;

/// <summary>
/// Summary description for ZaalManager
/// </summary>
public class ZaalManager
{
	private debioscoopEntities db;
  

	public ZaalManager(debioscoopEntities db)
	{
        this.db = db;
	}

    public void addZaal(zaal zaal)
    {
        db.zaals.Add(zaal);
        executeQuery();
    }

    public object getZalen()
    {
      

        var z2 = (from z in db.zaals
                 join b in db.bioscoops
                 on 
                 z.bioscoopbioscoop_id equals b.bioscoop_id 
                 select new {z.bioscoopbioscoop_id, b.naam, z.zaalnummer, z.rijen, z.zitting});
        var lijst = z2.ToList();
        return lijst;

    }

  

       public bool insertZaal(int bioscoopid, int zaalnummer, int rijen, int zitting)
    {
        bool succeed = false;
        try
        {
            zaal zaal1 = new zaal();
            zaal1.bioscoopbioscoop_id = bioscoopid;
            zaal1.zaalnummer = zaalnummer;
            zaal1.rijen = rijen;
            zaal1.zitting = zitting;
            db.zaals.Add(zaal1);
            db.SaveChanges();
        }
        catch (DbEntityValidationException e)
        {
            
            return succeed;
        }
        succeed = true;
        return succeed;
    }

    public bool alterZaal(int bioscoopid, int zaalnummer, int rijen, int zitting)
    {
        bool succeed = false;
        try
        {

            zaal zaal1 = db.zaals.Find(bioscoopid, zaalnummer);
            zaal1.bioscoopbioscoop_id = bioscoopid;
            zaal1.zaalnummer = zaalnummer;
            zaal1.rijen = rijen;
            zaal1.zitting = zitting;
            db.SaveChanges();
        }
        catch (DbEntityValidationException e)
        {

            return succeed;
        }
        succeed = true;
        return succeed;
    }


    public bool deleteZaal(int bioscoopid, int zaalnummer, int rijen, int zitting)
    {
        bool succeed = false;
        try
        {
            zaal zaal1 = new zaal();
            zaal1.bioscoopbioscoop_id = bioscoopid;
            zaal1.zaalnummer = zaalnummer;
            zaal1.rijen = rijen;
            zaal1.zitting = zitting;
            db.zaals.Remove(zaal1);
            db.SaveChanges();
        }
        catch (DbEntityValidationException e)
        {

            return succeed;
        }
        succeed = true;
        return succeed;
    }



    public void executeQuery()
    {
        try
        {
            db.SaveChanges();
        }
        catch (DbEntityValidationException e)
        {
            foreach (var eve in e.EntityValidationErrors)
            {
                System.Diagnostics.Debug.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                    eve.Entry.Entity.GetType().Name, eve.Entry.State);
                foreach (var ve in eve.ValidationErrors)
                {
                    System.Diagnostics.Debug.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                        ve.PropertyName, ve.ErrorMessage);
                }
            }
            throw;
        }
        catch (Exception e)
        {
            System.Diagnostics.Debug.WriteLine(e.InnerException);
        }
    }
	




 


}
	
